/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.yurendao.OSFileSystem.myList;

import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Administrator
 */
public class ListCellRendererDemo extends JApplet{

    private int number_of_nations = 7;
    private String[] nations = new String[]{"Denmark","Germany","China","India","Norway","UK","US"};
    private ImageIcon[] icons = new ImageIcon[number_of_nations];
    private ImageIcon[] bigicons = new ImageIcon[number_of_nations];

    private DefaultListModel listModel = new DefaultListModel();
    private JList jlistnations = new JList(listModel);
    private ListCellRenderer renderer = new MyListCellRenderer();

    private JSplitPane jsp = new JSplitPane();
    private JLabel jlblImage = new JLabel("",JLabel.CENTER);

    public ListCellRendererDemo() throws HeadlessException {
        for(int i=0;i<this.number_of_nations;i++){
            this.icons[i] = new ImageIcon("image\\24"+(i+1)+".jpg");
            this.listModel.addElement(new Object[]{icons[i],nations[i]});
            this.bigicons[i] = new ImageIcon("image\\25"+(i+1)+".jpg");
        }

        this.jlistnations.setCellRenderer(renderer);
        this.jlistnations.setPreferredSize(new Dimension(200,200));
        this.jsp.setLeftComponent(new JScrollPane(this.jlistnations));
        this.jsp.setRightComponent(this.jlblImage);

        this.jlistnations.setSelectedIndex(0);
        this.jlblImage.setIcon(this.bigicons[0]);

        this.add(jsp);

        jlistnations.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                jlblImage.setIcon(bigicons[jlistnations.getSelectedIndex()]);
            }
        });

    }
}
